# 2023.5.29
# Load libraries
library(Seurat)
library(tidyverse)
library(tidyseurat)
library(tidySingleCellExperiment)
library(ggpubr)
source("00_util_scripts/mod_seurat.R")

# load seurat objects ----------
sce_10x <- read_rds("CRC-I/data/crc10x_singler_ref.rds")

sobj <- read_rds("CRC-I/data/seekgene/crc2305.emtpydrop.rds")

sobj$Platform <- "Seekgene.STARsolo"

sobj |> VlnPlot("FCGR2B", group.by = "hpca_main")

sobj <- sobj |>
  quick_process_seurat(pcs = 25, res = 2)

DimPlot(sobj, group.by = "orig.ident")

sobj <- sobj |>
  mark_cell_type_singler(
    ref = sce_10x,
    sc_ref = TRUE,
    new_label = "zhang2020_main"
  )

DimPlot(sobj,
  group.by = "zhang2020_main", label = TRUE, label.box = TRUE,
  cols = DiscretePalette(5, "polychrome")
)

write_rds(sobj, "CRC-I/data/seekgene/crc2305.emtpydrop.rds")

# we choose zhang2020 as prefered ref
"label.fine,newtype
CD4-CCR7,CCR7-CD4-Tn
CD4-ANXA1,ANXA1-CD4-Tcm
CD4-GNLY,GNLY-CD4-Teff
CD4-TCF7,TCF7-CD4-Tmem
CD4-CXCR6,CXCR6-CD4-Trm (activated)
CD4-CXCR5,CXCR5-CD4-Tfh
CD4-GZMK,GZMK-CD4-Tem
CD4-IL23R,IL23R-CD4-Th17
CD4-CXCL13,CXCL13-CD4-Th1-like
CD4-FOXP3,FOXP3-CD4-Treg
CD4-IL10,IL10-CD4-Tfreg
CD4-CTLA4,CTLA4-CD4-Treg
CD8-LEF1,LEF1-CD8-Tn
CD8-GPR183,GPR183-CD8-Tcm
CD8-CX3CR1,CX3CR1-CD8-Teff
CD8-GZMK,GZMK-CD8-Tem
CD8-CD6,CD6-CD8-Trm
CD8-CD160,CD160-CD8-IEL
CD8-LAYN,LAYN-CD8-Tex
CD8-SLC4A10,SLC4A10-CD8-MAIT" |> read_delim() -> annotated_t_type

annotated_t_type |> write_csv('CRC-I/results/t.cell.type.annotation.csv')

# FCGR2B in CD8 T? -------
get_abundance_sc_long(sobj, 'FCGR2B') -> fcgr2b_expr

fcgr2b_expr <- sobj |> as_tibble() |>
  select(.cell, zhang2020_main) |>
  left_join(fcgr2b_expr) 

fcgr2b_expr <- fcgr2b_expr |> mutate(zhang2020_main = str_remove(zhang2020_main, ' cell'))

fcgr2b_expr |>
  ggplot(aes(zhang2020_main, .abundance_RNA, color = zhang2020_main)) +
  stat_summary(geom = 'col', fun = 'mean', fill = 'white', linewidth = 2) +
  stat_summary(geom = 'errorbar', fun.data = 'mean_cl_boot', width = .3) +
  ggpubr::theme_pubr() +
  labs(x = 'Cell type', y = 'Normalized expression', color = 'Cell type') +
  NoLegend()

g1 <- last_plot()

fcgr2b_expr |>
  filter(.abundance_RNA > 0) |>
  count(zhang2020_main) |>
  ggplot(aes(zhang2020_main, n, fill = zhang2020_main)) +
  geom_col() +
  ggpubr::theme_pubr() +
  labs(x = 'Cell type', y = 'Number of FCGR2B+ cells', color = 'Cell type') +
  NoLegend()

g2 <- last_plot()

g1 + g2 + patchwork::plot_annotation(title = 'FCGR2B expression in CRC TME immune cells')

# correct initial clustering --------
## init CD4 T-----------
sobj_cd4 <- sobj |>
  filter(zhang2020_main == "CD4 T cell") |>
  GetAssay() |>
  CreateSeuratObject()

sobj_cd4@meta.data <- sobj@meta.data |>
  filter(str_detect(zhang2020_main, "CD4 T cell"))

sobj_cd4 <- sobj_cd4 |>
  quick_process_seurat(pcs = 25, res = 2)

sobj_cd4 <- sobj_cd4 |>
  mark_cell_type_singler(
    ref = sce_10x,
    sc_ref = TRUE,
    new_label = "zhang2020_main"
  )

cd4_init <- sobj_cd4 |>
  as_tibble() |>
  mutate(
    RNA_snn_res.2 = as.character(RNA_snn_res.2),
    zhang2020_main = case_match(RNA_snn_res.2,
      "16" ~ "Myeloid cell",
      .default = zhang2020_main
    )
  ) |>
  select(zhang2020_main, .cell)

cd4_init |> count(zhang2020_main)

## inital CD8 T ----
sobj_cd8 <- sobj |>
  filter(zhang2020_main == "CD8 T cell") |>
  GetAssay() |>
  CreateSeuratObject()

sobj_cd8@meta.data <- sobj@meta.data |>
  filter(str_detect(zhang2020_main, "CD8 T cell"))

sobj_cd8 <- sobj_cd8 |>
  quick_process_seurat(pcs = 25, res = 2)

sobj_cd8 <- sobj_cd8 |>
  mark_cell_type_singler(
    ref = sce_10x,
    sc_ref = TRUE,
    new_label = "zhang2020_main"
  )

cd8_init <- sobj_cd8 |>
  as_tibble() |>
  select(.cell, zhang2020_main)

cd8_init |> count(zhang2020_main)

## initial B -----
sobj_b <- sobj |>
  filter(zhang2020_main == "B cell") |>
  GetAssay() |>
  CreateSeuratObject()

sobj_b@meta.data <- sobj@meta.data |>
  filter(str_detect(zhang2020_main, "B cell"))

sobj_b <- sobj_b |>
  quick_process_seurat(pcs = 25, res = 2)

sobj_b <- sobj_b |>
  mark_cell_type_singler(
    ref = sce_10x,
    sc_ref = TRUE,
    new_label = "zhang2020_main"
  )

bc_init <- sobj_b |>
  as_tibble() |>
  select(.cell, zhang2020_main)

bc_init |> count(zhang2020_main)

## init myeloid ---------
sobj_myl <- sobj |>
  filter(zhang2020_main == "Myeloid cell") |>
  GetAssay() |>
  CreateSeuratObject()

sobj_myl@meta.data <- sobj@meta.data |>
  filter(str_detect(zhang2020_main, "Myeloid"))

sobj_myl <- sobj_myl |>
  quick_process_seurat(pcs = 25, res = 2)

sobj_myl <- sobj_myl |>
  mark_cell_type_singler(
    ref = sce_10x,
    sc_ref = TRUE,
    new_label = "zhang2020_main"
  )

myl_init <- sobj_myl |>
  as_tibble() |>
  select(.cell, zhang2020_main)

myl_init |> count(zhang2020_main)

## init ILC ----
sobj_ilc <- sobj |>
  filter(zhang2020_main == "ILC") |>
  GetAssay() |>
  CreateSeuratObject()

sobj_ilc@meta.data <- sobj@meta.data |>
  filter(str_detect(zhang2020_main, "ILC"))

sobj_ilc <- sobj_ilc |>
  quick_process_seurat()

sobj_ilc <- sobj_ilc |>
  mark_cell_type_singler(
    ref = sce_10x,
    sc_ref = TRUE,
    new_label = "zhang2020_main"
  )

ilc_init <- sobj_ilc |>
  as_tibble() |>
  select(.cell, zhang2020_main)

ilc_init |> count(zhang2020_main)

## correct all ---------
all_init <- bind_rows(
  cd8_init, cd4_init, bc_init,
  myl_init, ilc_init
)

sobj <- sobj |>
  select(-zhang2020_main) |>
  left_join(all_init)

sobj |> DimPlot(group.by = "zhang2020_main") +
  ggtitle("Major tumor immune cell groups")

write_rds(sobj, "CRC-I/data/seekgene/empdrp.major.correct.rds")

sobj <- read_rds('CRC-I/data/seekgene/empdrp.major.correct.rds')

# find real subclusters --------
## real cd4 ---------
sce_cd4 <- sce_10x |> filter(label.main == "CD4 T cell")

sobj_cd4 <- sobj |>
  filter(str_detect(zhang2020_main, "CD4")) |>
  GetAssay() |>
  CreateSeuratObject()

sobj_cd4@meta.data <- sobj@meta.data |>
  filter(str_detect(zhang2020_main, "CD4 T cell"))

sobj_cd4 <- sobj_cd4 |>
  quick_process_seurat(pcs = 25, res = 2)

leiden_cd4 <- sobj_cd4 |>
  FindClusters(algorithm = 4)

## maunally define clusters
sobj_cd4 <- annotate_latents(leiden_cd4, 'CD4T')
  
sobj_cd4 |> DimPlot(group.by = 'latent_cluster',
                    cols = DiscretePalette(20)) +
  ggtitle('Newly defined CD4 T clusters')

sobj_cd4 <- sobj_cd4 |>
  mark_cell_type_singler(
    ref = sce_cd4,
    fine_label = TRUE,
    sc_ref = TRUE,
    new_label = "zhang2020_fine"
  )

sobj_cd4 |> DimPlot(group.by = "zhang2020_fine",
                    label = TRUE, label.box = TRUE, repel = TRUE)

## plot nominal CD4 markers in CD4 zhang2020
nominal_cd4mark <- c("CCR7", "IL23R", "CXCL13", "GNLY", "CXCR5", "TCF7", "ANXA1", "CTLA4", "CXCR6", "GZMK")

sobj_cd4 |> DotPlot(features = nominal_cd4mark)

sobj_cd4 |>
  FindAllMarkers(
    features = nominal_cd4mark,
    only.pos = TRUE
  ) |>
  filter(p_val_adj < .05) |>
  group_by(gene) |>
  slice_max(avg_log2FC)

## determine stat significant markers
all_cd4mark <- sobj_10x_cd4 |>
  FindAllMarkers(only.pos = TRUE) |>
  filter(p_val_adj < .05) |>
  group_by(gene) |>
  slice_max(avg_log2FC)

all_cd4mark |>
  as_tibble() |>
  filter(p_val_adj < .05) |>
  group_by(gene) |>
  slice_max(avg_log2FC) |>
  group_by(cluster) |>
  slice_max(avg_log2FC, n = 2)

## IL23R+ Th17 marker in zhang2018
th17_marker <- c("IL23R", "RORC", "IL17A", "FURIN", "CTSH", "CCR6", "KLRB1", "ITGAE", "CAPG")

sobj_cd4 |>
  FindAllMarkers(
    features = th17_marker,
    only.pos = TRUE
  ) |>
  filter(p_val_adj < .05)

## TCF7+ Tmem marker in zhang2018
tcf7mem_marker <- c("TCF7", "CCR7", "RGS1", "CD69")

sobj_cd4 |>
  FindAllMarkers(
    features = tcf7mem_marker,
    only.pos = TRUE
  ) |>
  filter(p_val_adj < .05)

cd4_final <- sobj_cd4 |>
  as_tibble() |>
  mutate(
    seurat_clusters = as.character(seurat_clusters),
    zhang2020_fine = case_match(seurat_clusters,
      "10" ~ "CXCR5-CD4-Tfh",
      "8" ~ "TCF7-CD4-Tmem",
      .default = zhang2020_fine
    )
  ) |>
  select(zhang2020_fine, .cell, latent_cluster)

sobj_cd4 |>
  select(-zhang2020_fine) |>
  left_join(cd4_final) |>
  DimPlot(
    group.by = "zhang2020_fine", label = TRUE,
    label.box = TRUE, repel = TRUE
  )

sobj_cd4 |> DimPlot(
  group.by = "zhang2020_fine", label = TRUE,
  label.box = TRUE
)

cd4_final |> count(zhang2020_fine)

### CCR4+ Treg?
ccr4treg_marker <- c("CCR4", "FOXP3", "IL10", "IL35")

sobj_cd4 |>
  FindAllMarkers(
    features = "CCR4",
    only.pos = TRUE
  ) |>
  filter(p_val_adj < .05)

sobj_cd4 |> VlnPlot(c("CCR4", "FOXP3"), group.by = "zhang2020_fine")

## CD8 -------
sce_10x_cd8 <- sce_10x |>
  filter(str_detect(label.main, "CD8"))

sobj_cd8 <- sobj |>
  filter(zhang2020_main == "CD8 T cell") |>
  GetAssay() |>
  CreateSeuratObject()

sobj_cd8@meta.data <- sobj@meta.data |>
  filter(zhang2020_main == "CD8 T cell")

sobj_cd8 <- sobj_cd8 |>
  quick_process_seurat(pcs = 25, res = 2) |>
  FindClusters(algorithm = 4)

sobj_cd8 <- annotate_latents(sobj_cd8, 'CD8T')

sobj_cd8 |>
  DimPlot(group.by = 'latent_cluster',
          cols = DiscretePalette(20)) +
  ggtitle('Newly defined CD8 T clusters')

sobj_cd8 <- sobj_cd8 |>
  mark_cell_type_singler(
    ref = sce_10x_cd8,
    fine_label = TRUE,
    sc_ref = TRUE,
    new_label = "zhang2020_fine"
  )

#### GZMK+ Tem markers-----
gzmk_marker <- c("GZMK", "CD44", "CXCR3", "CXCR4")

sobj_cd8 |> FindAllMarkers(features = gzmk_marker, only.pos = TRUE)

#### CD60 IEL markers -----
cd160_marker <- c("CD160", "KIR2DL4", "TMIGD2", "IKZF2", "ENTPD1", "CD69", "ITGAE", "KLRC1", "KLRC2", "KLRC3", "NR4A1", "NR4A2", "NR4A3")

sobj_cd8 |> FindAllMarkers(features = cd160_marker, only.pos = TRUE)

#### manually edit clusters ------
sobj_cd8 |> DimPlot(group.by = "zhang2020_fine", label = TRUE, label.box = TRUE)

sobj_cd8 <- sobj_cd8 |>
  mutate(zhang2020_fine = case_match(as.character(seurat_clusters),
    "9" ~ "CD160-CD8-IEL",
    c("6") ~ "GZMK-CD8-Tem",
    .default = zhang2020_fine
  ))

cd8_final <- sobj_cd8 |>
  as_tibble() |>
  select(zhang2020_fine, latent_cluster, .cell)

## myeloid cells ---------
sobj_myld <- sobj |>
  filter(zhang2020_main == "Myeloid cell") |>
  GetAssay() |>
  CreateSeuratObject()

sobj_myld@meta.data <- sobj@meta.data |>
  filter(zhang2020_main == "Myeloid cell")

sobj_myld <- sobj_myld |>
  quick_process_seurat(pcs = 25, res = 2) |>
  FindClusters(algorithm = 4)

sobj_myld <- annotate_latents(sobj_myld, 'Myeloid')

sce_10x_m <- sce_10x |>
  filter(str_detect(label.main, "Myeloid"))

sobj_myld <- sobj_myld |>
  mark_cell_type_singler(
    ref = sce_10x_m,
    fine_label = TRUE,
    sc_ref = TRUE,
    new_label = "zhang2020_fine"
  )

#### UMAP isolated population ------
FindMarkers(sobj_myld, ident.1 = 12, only.pos = TRUE) |> head()

# should be neutrophil

#### SPP1+ TAM marker --------
spp1_marker <- c("SPP1", "CEBPB", "IL1RN", "OLR1", "CXCL2", "VEGFA", "EREG", "GPNMB", "PHLDA1", "AQP9", "TNS3", "NDRG1")

sobj_myld |> DotPlot(features = spp1_marker)
sobj_myld |> FindAllMarkers(features = spp1_marker, only.pos = TRUE)

sobj_myld |> FeaturePlot(c("SPP1", "C1QC"))

sobj_myld |> count(zhang2020_fine, sort = TRUE)

DimPlot(sobj_myld,
  group.by = "zhang2020_fine",
  cols = DiscretePalette(12)
)

DimPlot(sobj_myld,
        group.by = "latent_cluster",
        cols = DiscretePalette(20)
) +
  ggtitle('Newly defined myeloid clusters')

myld_final <- sobj_myld |>
  as_tibble() |>
  select(.cell, zhang2020_fine, latent_cluster)

### macrophages --------
sobj_mf <- sobj_myld |>
  filter(str_detect(zhang2020_fine, "TAM|Macro")) |>
  GetAssay() |>
  CreateSeuratObject()

sobj_mf@meta.data <- sobj_myld@meta.data |>
  filter(str_detect(zhang2020_fine, "TAM|Macro"))

sobj_mf <- sobj_mf |>
  quick_process_seurat() |>
  FindNeighbors(reduction = "harmony", dims = 1:25) |>
  FindClusters(resolution = 2)

sobj_mf |> DimPlot(label = TRUE, label.box = TRUE)

sobj_mf <- sobj_mf |> select(-zhang2020_fine)

sobj_mf <- sobj_mf |>
  mark_cell_type_singler(
    ref = sce_10x_m,
    fine_label = TRUE,
    sc_ref = TRUE,
    new_label = "zhang2020_fine"
  )

sobj_mf |> DotPlot(features = spp1_marker)
sobj_mf |> FindAllMarkers(features = c1qc_marker, only.pos = TRUE)

sobj_mf |> count(seurat_clusters, zhang2020_fine)

##### IL1B+ Macro marker ---------
il1b_marker <- c("C1QC", "C1QA", "C1QB", "IL1B", "HLA-DQA1", "HLA-DQA2", "HLA-DQB1", "GPR183", "CD83", "CLEC10A", "MRC1", "CCL3L1")

sobj_mf |>
  Seurat::AddModuleScore(list(c1qc_marker), name = "il1b_marker") |>
  summarise(score = mean(il1b_marker1), .by = seurat_clusters) |>
  arrange(score)

sobj_mf |> DotPlot(features = il1b_marker)
sobj_mf |> FindAllMarkers(features = il1b_marker, only.pos = TRUE)

sobj_mf |> FeaturePlot(c("SPP1", "C1QC"))

sobj_mf |> count(zhang2020_fine, sort = TRUE)

#### manually annotate 2 types --------
sobj_mf <- sobj_mf |>
  mutate(zhang2020_fine = case_match(as.numeric(seurat_clusters),
    2 ~ "TAM-SPP1",
    c(1, 15) ~ "Macro-IL1B",
    .default = zhang2020_fine
  ))

DimPlot(sobj_mf,
  group.by = "zhang2020_fine",
  label = TRUE,
  label.box = TRUE,
  cols = DiscretePalette(n = 12, palette = "polychrome")
)

meta_mf <- sobj_mf@meta.data |>
  as_tibble(rownames = "barcodes")


## B cells --------
sobj_b <- sobj |>
  filter(zhang2020_main == "B cell") |>
  GetAssay() |>
  CreateSeuratObject()

sobj_b@meta.data <- sobj@meta.data |>
  filter(zhang2020_main == "B cell")

sobj_b <- sobj_b |>
  quick_process_seurat(pcs = 25, res = 2) |>
  FindClusters(algorithm = 4)

sce_10x_b <- sce_10x |>
  filter(str_detect(label.main, "B cell"))

sobj_b <- sobj_b |> annotate_latents('B_cell')

sobj_b <- sobj_b |>
  mark_cell_type_singler(
    ref = sce_10x_b,
    fine_label = TRUE,
    sc_ref = TRUE,
    new_label = "zhang2020_fine"
  )

sobj_b |> DimPlot(group.by = 'latent_cluster',
                  cols = DiscretePalette(20)) +
  ggtitle('Newly defined B cell clusters')

sobj_b |> DimPlot(label = TRUE, label.box = TRUE, group.by = "zhang2020_fine")

b_final <- sobj_b@meta.data |>
  as_tibble(rownames = ".cell") |>
  select(.cell, zhang2020_fine, latent_cluster)

## ILC final -------
sobj_ilc <- sobj |>
  filter(zhang2020_main == "ILC") |>
  GetAssay() |>
  CreateSeuratObject()

sobj_ilc@meta.data <- sobj@meta.data |>
  filter(zhang2020_main == "ILC")

sobj_ilc <- sobj_ilc |>
  quick_process_seurat()

sce_ilc <- sce_10x |>
  filter(str_detect(label.main, "ILC"))

sobj_ilc <- sobj_ilc |>
  mark_cell_type_singler(
    ref = sce_ilc,
    fine_label = TRUE,
    sc_ref = TRUE,
    new_label = "zhang2020_fine"
  )

sobj_ilc |> DimPlot(label = TRUE, label.box = TRUE, group.by = "zhang2020_fine")

sobj_ilc <- sobj_ilc |> annotate_latents('ILC')

sobj_ilc |> DimPlot(group.by = "latent_cluster")

ilc_final <- sobj_ilc@meta.data |>
  as_tibble(rownames = ".cell") |>
  select(.cell, zhang2020_fine, latent_cluster)

## merge all ------------
meta_reanno <- cd4_final |>
  bind_rows(cd8_final, b_final, myld_final, ilc_final)

sobj <- sobj |> left_join(meta_reanno)

sobj |> count(zhang2020_fine)

fine2main <- sce_10x |>
  count(label.main, label.fine) |>
  mutate(zhang2020_main = label.main, zhang2020_fine = label.fine, .keep = "none")

sobj <- sobj |>
  select(-zhang2020_main) |>
  left_join(fine2main)

sobj |> write_rds("CRC-I/data/seekgene/crc-starsolo-annotated.rds")

# prepare for ecotyper -----------
seek_eco_anno <- sobj |>
  as_tibble() |>
  mutate(ID = .cell, CellType = manual_main, Sample = orig.ident, .keep = "none") |>
  write_tsv("CRC-I/results/seek3_eco_anno.tsv")

sobj@assays$RNA@data %>%
  as.data.frame() %>%
  rownames_to_column("Cell-ID") |>
  write_tsv("CRC-I/results/seek3_immune_mat.tsv")

meta_10x <- sobj_10x@meta.data |>
  as_tibble(rownames = "ID")

meta_10x |>
  mutate(
    ID = ID,
    CellType = case_when(
      str_detect(Sub_Cluster, "PlasmaB") ~ "PCs",
      Global_Cluster == "B cell" ~ "B.cells",
      str_detect(Sub_Cluster, "NK|ILC") ~ "NK.cells",
      str_detect(Sub_Cluster, "Mast") ~ "Mast.cells",
      str_detect(Sub_Cluster, "cDC|pDC") ~ "Dendritic.cells",
      str_detect(Sub_Cluster, "Macro|Mono|TAM") ~ "Monocytes.and.Macrophages",
      Global_Cluster == "CD4 T cell" ~ "CD4.T.cells",
      Global_Cluster == "CD8 T cell" ~ "CD8.T.cells"
    ),
    Sample = orig.ident,
    .keep = "none"
  ) |>
  write_tsv("CRC-I/results/zhang2020_10x_eco_anno.tsv")

sobj_10x@assays$RNA@data %>%
  as.data.frame() %>%
  rownames_to_column("Cell-ID") |>
  write_tsv("CRC-I/results/zhang2020_10x_immune_mat.tsv")

## smart-seq2 data
sobj_smart2020 <- read_rds("CRC-I/data/smart_tpm.rds") |>
  filter(Tissue == "T" & Global_Cluster != "Malignant cell" & Global_Cluster != "Epithelial cell" & Global_Cluster != "Fibroblast")

meta_sm2020 <- sobj_smart2020@meta.data |>
  as_tibble(rownames = "ID")

meta_sm2020 |>
  count(Sub_Cluster, Global_Cluster) |>
  DT::datatable()

meta_sm2020 |>
  mutate(
    ID = ID,
    CellType = case_when(
      str_detect(Sub_Cluster, "PlasmaB") ~ "PCs",
      Global_Cluster == "B cell" ~ "B.cells",
      str_detect(Sub_Cluster, "NK|ILC") ~ "NK.cells",
      str_detect(Sub_Cluster, "Mast") ~ "Mast.cells",
      str_detect(Sub_Cluster, "cDC|pDC") ~ "Dendritic.cells",
      str_detect(Sub_Cluster, "Macro|Mono|TAM") ~ "Monocytes.and.Macrophages",
      Global_Cluster == "CD4 T cell" ~ "CD4.T.cells",
      Global_Cluster == "CD8 T cell" ~ "CD8.T.cells"
    ),
    # Sample = orig.ident,
    .keep = "none"
  ) |>
  write_tsv("CRC-I/results/zhang2020_smart_eco_anno.tsv")

sobj_smart2020@assays$RNA@data %>%
  as.data.frame() %>%
  rownames_to_column("Cell-ID") |>
  write_tsv("CRC-I/results/zhang2020_smart_immune_mat.tsv")


### smart2018 -------
sobj_smart2018 <- read_rds("CRC-I/data/zhang2018_Count.rds") |>
  filter(tissue == "Tumor" & str_detect(majorCluster, "CD4|CD8"))

meta_sm2018 <- sobj_smart2018@meta.data |>
  as_tibble(rownames = "ID")

meta_sm2018 |>
  count(majorCluster) |>
  DT::datatable()

meta_sm2018 |>
  mutate(
    ID = ID,
    CellType = case_when(str_detect(majorCluster, "CD4") ~ "CD4.T.cells",
      .default = "CD8.T.cells"
    ),
    Sample = Patient_ID,
    .keep = "none"
  ) |>
  write_tsv("CRC-I/results/zhang2018_smart_eco_anno.tsv")

sobj_smart2018 <- sobj_smart2018 |>
  NormalizeData()

sobj_smart2018@assays$RNA@data[1:5, 1:5]

sobj_smart2018@assays$RNA@data %>%
  as.data.frame() %>%
  rownames_to_column("Cell-ID") |>
  write_tsv("CRC-I/results/zhang2018_smart_immune_mat.tsv")
